unit uMenu;

interface

uses
  zglHeader,
  uGUI,
  uGlobal;

type
  TMenu = class
    protected
      font : zglPFont;
      gui : TGUIManager;
      backgroundTex : zglPTexture;
      camera : zglPCamera2d;
      global : TGlobal;
      snd_menu : zglPSound;
      snd_id : integer;
    public
      constructor Create(_global : TGlobal);
      procedure How2PlayDisplay;
      procedure CreditsDisplay;
      procedure ScoresDisplay;
      procedure process;
      procedure draw;
      destructor Destroy; override;
  end;

  TExitBtn = class(TButton)
    public
      constructor Create(const x0, y0 : single; w0, h0 : word; layer0 : byte; font0 : zglPFont;
                        text0 : string; gui0 : TGUIManager);
      procedure onClick(data : pointer); override;
  end;
  TGameBtn = class(TButton)
    private
      global : TGlobal;
      _menu : TMenu;
    public
      constructor Create(const x0, y0 : single; w0, h0 : word; layer0 : byte; font0 : zglPFont;
                        text0 : string; gui0 : TGUIManager; _global : TGlobal; menu: TMenu);
      procedure onClick(data : pointer); override;
  end;
  TCreditsBtn = class(TButton)
    public
      constructor Create(const x0, y0 : single; w0, h0 : word; layer0 : byte; font0 : zglPFont;
                        text0 : string; gui0 : TGUIManager);
      procedure onClick(data : pointer); override;
  end;
  THow2PlayBtn = class(TButton)
    public
      constructor Create(const x0, y0 : single; w0, h0 : word; layer0 : byte; font0 : zglPFont;
                        text0 : string; gui0 : TGUIManager);
      procedure onClick(data : pointer); override;
  end;
  TScoresBtn = class(TButton)
    public
      constructor Create(const x0, y0 : single; w0, h0 : word; layer0 : byte; font0 : zglPFont;
                        text0 : string; gui0 : TGUIManager);
      procedure onClick(data : pointer); override;
  end;
  TBackBtn = class(TButton)
    public
      constructor Create(const x0, y0 : single; w0, h0 : word; layer0 : byte; font0 : zglPFont;
                        text0 : string; gui0 : TGUIManager);
      procedure onClick(data : pointer); override;
  end;
  TTextSm = class(TText)
    protected
      color : Cardinal;
    public
      constructor Create(const x0, y0 : single; w0, h0 : word;
                           layer0 : byte; font0 : zglPFont; text0 : string; color0 : Cardinal);
      procedure draw; override;
  end;

implementation

constructor TExitBtn.Create(const x0, y0 : single; w0, h0 : word; layer0 : byte; font0 : zglPFont;
                        text0 : string; gui0 : TGUIManager);
begin
  inherited Create(x0, y0, w0, h0, layer0, font0, text0, gui0);
end;

procedure TExitBtn.onClick(data: Pointer);
begin
  zgl_Exit;
end;

constructor TGameBtn.Create(const x0, y0 : single; w0, h0 : word; layer0 : byte; font0 : zglPFont;
                        text0 : string;  gui0 : TGUIManager; _global : TGlobal; menu: TMenu);
begin
  inherited Create(x0, y0, w0, h0, layer0, font0, text0, gui0);
  _menu := menu;
  global := _global;
end;
procedure TGameBtn.onClick(data: Pointer);
begin
  global.mode := mgame;
  global.StopAllMusic;
end;

constructor TCreditsBtn.Create(const x0, y0 : single; w0, h0 : word; layer0 : byte; font0 : zglPFont;
                        text0 : string; gui0 : TGUIManager);
begin
  inherited Create(x0, y0, w0, h0, layer0, font0, text0, gui0);
end;
procedure TCreditsBtn.onClick(data: Pointer);
begin
  gui.displayLayer := 3;
end;

constructor THow2PlayBtn.Create(const x0, y0 : single; w0, h0 : word; layer0 : byte; font0 : zglPFont;
                        text0 : string; gui0 : TGUIManager);
begin
  inherited Create(x0, y0, w0, h0, layer0, font0, text0, gui0);
end;
procedure THow2PlayBtn.onClick(data: Pointer);
begin
  gui.displayLayer := 1;
end;

constructor TScoresBtn.Create(const x0, y0 : single; w0, h0 : word; layer0 : byte; font0 : zglPFont;
                        text0 : string; gui0 : TGUIManager);
begin
  inherited Create(x0, y0, w0, h0, layer0, font0, text0, gui0);
end;
procedure TScoresBtn.onClick(data: Pointer);
begin
  gui.displayLayer := 2;
end;

constructor TBackBtn.Create(const x0, y0 : single; w0, h0 : word; layer0 : byte; font0 : zglPFont;
                        text0 : string; gui0 : TGUIManager);
begin
  inherited Create(x0, y0, w0, h0, layer0, font0, text0, gui0);
end;
procedure TBackBtn.onClick(data: Pointer);
begin
  gui.displayLayer := 0;
end;

constructor TTextSm.Create(const x0, y0 : single; w0, h0 : word;
                           layer0 : byte; font0 : zglPFont; text0 : string; color0 : Cardinal);
begin
  inherited Create(x0, y0, w0, h0, layer0, font0, text0);
  color := color0;
end;
procedure TTextSm.draw;
begin
  text_DrawEx(font, colRect.X, colRect.Y, 1/3, 0.0, text, 255, color);
end;

//---------------------------MENU--------------------------//

constructor TMenu.Create(_global : TGlobal);
//var  camera : zglPCamera2d;
begin

  global := _global;
  camera := cam2d_get;
  camera.Zoom.X := 2;
  camera.Zoom.Y := 2;
  camera.X := -640/4;
  camera.Y := -480/4;
  cam2d_set(camera);
  gui := TGUImanager.Create;
  font := font_LoadFromFile(FontPath + 'standard 07_57-Regular-18pt.zfi');
  tex_Filter( font.Pages[ 0 ], TEX_CLAMP or TEX_FILTER_NEAREST or TEX_CONVERT_TO_POT or TEX_CALCULATE_ALPHA);
  snd_menu := snd_LoadFromFile(SoundPath + 'metol_riff1.wav');

  gui.elementList.Add(TGameBtn.Create(30/camera.Zoom.X, 135/camera.Zoom.Y, 50, 10, 0, font, 'Game', gui, global, self));
  gui.elementList.Add(THow2PlayBtn.Create(30/camera.Zoom.X, 170/camera.Zoom.Y, 60, 10, 0, font, 'How To Play', gui));
  //gui.elementList.Add(TScoresBtn.Create(30/camera.Zoom.X, 205/camera.Zoom.Y, 50, 10, 0, font, 'Scores', gui));
  gui.elementList.Add(TCreditsBtn.Create(30/camera.Zoom.X, 205/camera.Zoom.Y, 50, 10, 0, font, 'Credits', gui));
  gui.elementList.Add(TExitBtn.Create(30/camera.Zoom.X, 240/camera.Zoom.Y, 50, 10, 0, font, 'Exit', gui));
  gui.displayLayer := 0;

  How2PlayDisplay;
  ScoresDisplay;
  CreditsDisplay;

  backgroundTex := Tex_LoadFromFile(GraphicsPath + 'metolsplscr-38.png', $FF00FF, TEX_DEFAULT_2D or TEX_FILTER_LINEAR or TEX_FILTER_NEAREST);

end;

procedure TMenu.How2PlayDisplay;
begin
  gui.elementList.add(TTextSm.Create(32/camera.Zoom.X, 135/camera.Zoom.Y, 1, 1, 1,
                      font, 'Collect AMPLIFIERS.', $FFFFFF));
  gui.elementList.add(TTextSm.Create(32/camera.Zoom.X, 153/camera.Zoom.Y, 1, 1, 1,
                      font, '10', $FFFF00));
  gui.elementList.add(TTextSm.Create(50/camera.Zoom.X, 153/camera.Zoom.Y, 1, 1, 1,
                      font, 'AMPS in a row give you a BONUS.', $FFFFFF));
  gui.elementList.add(TTextSm.Create(32/camera.Zoom.X, 171/camera.Zoom.Y, 1, 1, 1,
                      font, 'BONUS is breaking', $FFFFFF));
  gui.elementList.add(TTextSm.Create((32)/camera.Zoom.X, 189/camera.Zoom.Y, 1, 1, 1,
                      font, 'a guitar shop.', $FFFFFF));
  gui.elementList.add(TTextSm.Create(380/camera.Zoom.X, 135/camera.Zoom.Y, 1, 1, 1,
                      font, 'To use BONUS press', $FFFFFF));
  gui.elementList.add(TTextSm.Create((380+151)/camera.Zoom.X, 135/camera.Zoom.Y, 1, 1, 1,
                      font, 'Z.', $FFFF00));
  gui.elementList.add(TTextSm.Create(380/camera.Zoom.X, 164/camera.Zoom.Y, 1, 1, 1,
                      font, 'Press UP to JUMP.', $FFFFFF));
  gui.elementList.add(TTextSm.Create((380+75)/camera.Zoom.X, 190/camera.Zoom.Y, 1, 1, 1,
                      font, 'Press SPACE for PAUSE.', $FFFFFF));
  gui.elementList.add(TTextSm.Create((380+100)/camera.Zoom.X, 225/camera.Zoom.Y, 1, 1, 1,
                      font, 'Have a nice game!', $FFFFFF));
  gui.elementList.Add(TBackBtn.Create(500/camera.Zoom.X, 279/camera.Zoom.Y, 50, 10, 1, font, 'Back', gui));
end;

procedure TMenu.CreditsDisplay;
begin
  gui.elementList.add(TTextSm.Create(32/camera.Zoom.X, 135/camera.Zoom.Y, 1, 1, 3,
                      font, 'Mikhail ''mitch'' Chalyshev:', $FFFFFF));
  gui.elementList.add(TTextSm.Create((32+130)/camera.Zoom.X, 153/camera.Zoom.Y, 1, 1, 3,
                      font, 'code, art, music.', $FFFFFF));
  gui.elementList.add(TTextSm.Create(32/camera.Zoom.X, 171/camera.Zoom.Y, 1, 1, 3,
                      font, 'interphx: code.', $FFFFFF));
  gui.elementList.add(TTextSm.Create(380/camera.Zoom.X, 180/camera.Zoom.Y, 1, 1, 3,
                      font, 'engine: ZenGL', $FFFFFF));
  gui.elementList.add(TTextSm.Create(380/camera.Zoom.X, 200/camera.Zoom.Y, 1, 1, 3,
                      font, 'glass crash sound: themfish', $FFFFFF));
  gui.elementList.Add(TBackBtn.Create(475/camera.Zoom.X, 253/camera.Zoom.Y, 50, 10, 3, font, 'Back', gui));
end;

procedure TMenu.ScoresDisplay;
begin
  gui.elementList.add(TTextSm.Create(380/camera.Zoom.X, 135/camera.Zoom.Y, 1, 1, 2,
                      font, 'not working', $FFFFFF));
  gui.elementList.Add(TBackBtn.Create(475/camera.Zoom.X, 171/camera.Zoom.Y, 50, 10, 2, font, 'Back', gui));
end;

destructor TMenu.Destroy;
begin
gui.Free;
inherited;
end;

procedure TMenu.process;
var state : integer;
begin
  state := snd_Get( snd_menu, snd_id, SND_STATE_PLAYING);
  if state = 0 then
    snd_id := snd_play(snd_menu);

  gui.process;
end;

procedure TMenu.draw;
begin
  ssprite2d_draw(backgroundTex, 0, 0, 320, 240, 0);
  gui.draw;
end;

end.
